/***
 *   @title qz
 *   @description <description class purpose>
 *   @author yichen
 *   @version 1.0.0
 *   @create 2023/9/19 0019
 **/
package io.gitee.wokaixin.yunque.message.service;

import io.gitee.wokaixin.yunque.driver.*;
import io.gitee.wokaixin.yunque.message.proto.*;
import io.gitee.wokaixin.yunque.message.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.nio.*;


//@Service
public class MessageService extends CommonService {

    public boolean send(SendAct.SendActProto sendAct, byte[] msg) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(msg.length + 2);
        byteBuffer.put((byte) sendAct.getNumber());
        byteBuffer.put((byte) SendType.SendTypeProto.MESSAGE_VALUE);
        byteBuffer.put(msg);
        byte[] arr = byteBuffer.array();
        return super.getDriver().sendTo(arr);
//        return driver. sendTo( arr);

    }

    public boolean send(SendAct.SendActProto sendAct, MessagesPub.MessagesPubProto.Builder msg) {
        return send(sendAct, msg.build().toByteArray());
    }

    /***
     *   @author: yichen
     *   @Date: 2023/9/19 0019
     *   @Description: 每条消息推给所有人
     *   @param
     *   @return: null
     **/

    public boolean sendAll(MessagesPub.MessagesPubProto.Builder msg) {
        return send(SendAct.SendActProto.SENDALL, msg.build().toByteArray());
    }

    /***
     *   @author: yichen
     *   @Date: 2023/9/19 0019
     *   @Description: 遍历消息推送给每条消息的接收人
     *   @param
     *   @return: null
     **/

    public boolean sendList(MessagesPub.MessagesPubProto.Builder msg) {
        return send(SendAct.SendActProto.SENDLIST, msg.build().toByteArray());
    }

    /***
     *   @author: yichen
     *   @Date: 2023/9/19 0019
     *   @Description: 遍历消息推送给指定接收人
     *   @param
     *   @return: null
     **/

    public boolean sendUid(MessagesPub.MessagesPubProto.Builder msg, String uid) {
        msg.setUid(uid);
        return send(SendAct.SendActProto.SENDUID, msg);
    }

    /***
     *   @author: yichen
     *   @Date: 2023/9/19 0019
     *   @Description: 遍历消息推送给指定接收人 的某个设备ID
     *   @param
     *   @return: null
     **/

    public boolean sendUid(MessagesPub.MessagesPubProto.Builder msg, String uid, String did) {
        msg.setUid(uid);
        msg.setDid(did);
        return this.send(SendAct.SendActProto.SENDUIDDID, msg);
    }

    /***
     *   @author: yichen
     *   @Date: 2023/9/19 0019
     *   @Description: 遍历消息推送给指定多个接收人
     *   @param
     *   @return: null
     **/

    public boolean sendUids(MessagesPub.MessagesPubProto.Builder msg, String[] uidlist) {
        String uids = String.join(",", uidlist);
        msg.setUid(uids);
        return this.send(SendAct.SendActProto.SENDUIDS, msg);
    }

}
